package com.bj58.ecdata.mailreport.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bj58.ecdata.mailreport.cache.ServiceBeanFactory;
import com.bj58.ecdata.mailreport.db.CommonDao;
import com.bj58.ecdata.mailreport.model.IEMain;
import com.bj58.ecdata.mailreport.model.IESend;
import com.bj58.ecdata.mailreport.model.IETable;
import com.bj58.ecdata.mailreport.model.IETableSearch;
import com.bj58.ecdata.mailreport.model.IRDbConfig;
import com.bj58.ecdata.mailreport.util.MyPathUtil;
import com.bj58.ecdata.mailreport.util.VelocityTools;



public class IntEmailMainService {
	private IEMainService IEMainService = (IEMainService) ServiceBeanFactory.get(IEMain.class, IEMainService.class);
	private IESendService IESendService = (IESendService) ServiceBeanFactory.get(IESend.class,IESendService.class);
	private IETableService IETableService = (IETableService) ServiceBeanFactory.get(IETable.class,IETableService.class);
	private IETableSearchService IETableSearchService = (IETableSearchService) ServiceBeanFactory.get(IETableSearch.class,IETableSearchService.class);
	private DbConfigService IRDbConfigService = (DbConfigService) ServiceBeanFactory.get(IRDbConfig.class,DbConfigService.class);
	
	private CommonDao isDao = new CommonDao();
	
	static String template1 = MyPathUtil.getIEPath()+"addTemplate1.html"; //汇总模板
	static String template2 = MyPathUtil.getIEPath()+"addTemplate2.html"; //详细指标模板
	static String template3 = MyPathUtil.getIEPath()+"addTemplate3.html"; //分城市指标模板
	static String template4 = MyPathUtil.getIEPath()+"addTemplate4.html"; //汇总(分类)模板
	static String template5 = MyPathUtil.getIEPath()+"addTemplate5.html"; //分类指标模板
	static String template6 = MyPathUtil.getIEPath()+"addTemplate6.html"; //分类指标模板
	
	public String getAddTemplate(int type, int rowCount, int colCount ,int colCount2,int rowCount2, int tableCount)
			throws Exception {
		
		List<IRDbConfig> dbList = IRDbConfigService.getAllDb();  //数据库列表
		
		Map<String,Object> conMap = new HashMap<String,Object>();
		conMap.put("rowCount", rowCount);
		conMap.put("colCount", colCount);
		conMap.put("colCount2", colCount2);
		conMap.put("rowCount2", rowCount2);
		conMap.put("dbList", dbList);
		
		String main = "";
		switch (type) {
		case 1:  //汇总模板
			main = MyPathUtil.getTemByPath(template1);
			break;
		case 2: //详细指标模板
			main = MyPathUtil.getTemByPath(template2);
			break;
		case 3: //按照行列二维分组指标模板
			main = MyPathUtil.getTemByPath(template3);
			break;
		case 4: //列分类组合汇总指标模板
			main = MyPathUtil.getTemByPath(template4);
			break;
		case 5: //分类指标模板
			main = MyPathUtil.getTemByPath(template5);
			break;
		case 6: //分类指标模板
			main = MyPathUtil.getTemByPath(template6);
			break;
		}
		
		String content  = VelocityTools.render(main, conMap);
		content = content.replaceAll("#tableCount", String.valueOf(tableCount));
		content = content.replaceAll("#templateTypeValue", String.valueOf(type));
		
		return content;
	}

	public int addNewEmail(IEMain baseMain, IESend send,List<IETable>  tableList,Map<IETable,List<IETableSearch>>  contentMap) throws Exception {
		//插入IEMain
		int mainId = 0;
		try{
			mainId = (Integer) IEMainService.add(baseMain);
			if(mainId == 0){
				return 0;
			}
		}catch (Exception e) {
			e.printStackTrace();
				IEMainService.delete(mainId);
			return 0;
		}
		
		//插入IESend
		int sendId = 0;
		try{
			send.setMainId(mainId);
			sendId =  (Integer) IESendService.add(send);
			if(sendId == 0){
				IESendService.delete(sendId);
				IEMainService.delete(mainId);
				return 0;
			}
		}catch (Exception e) {
			e.printStackTrace();
			IEMainService.delete( mainId);
			IESendService.delete(sendId);
			return 0;
		}
		
		//批量插入邮件内容配置 tableList && tableSearchList
		try{
			for(IETable table : tableList){
				table.setMainId(mainId);
				int tableId = (Integer) IETableService.add(table);
				List<IETableSearch> tableSearchList = contentMap.get(table);
				for (IETableSearch vo : tableSearchList) {
					vo.setTableId(tableId);
					vo.setMainId(mainId);
				}
				boolean result = IETableSearchService.saveList(tableSearchList);
				if(!result){
					IEMainService.delete(mainId);
					IESendService.delete(sendId);
					IETableService.deleteByCons("  and mainId =  " + mainId);
					IETableSearchService.deleteByCons(" and mainId =  " + mainId);
					return 0;
				}
			}
			
//			for(Iterator<IETable> it = contentMap.keySet().iterator(); it.hasNext();){
//				IETable table = it.next();
//				table.setMainId(mainId);
//				int tableId = (Integer) IETableService.save(table);
//				List<IETableSearch> tableSearchList = contentMap.get(table);
//				for (IETableSearch vo : tableSearchList) {
//					vo.setTableId(tableId);
//					vo.setMainId(mainId);
//				}
//				boolean result = IETableSearchService.saveList(tableSearchList);
//				if(!result){
//					IEMainService.delete(IEMain.class, mainId);
//					IESendService.delete(IESend.class, sendId);
//					IETableService.deleteByCons("  and mainId =  " + mainId);
//					IETableSearchService.deleteByCons(" and mainId =  " + mainId);
//					return false;
//				}
//			}
		}catch (Exception e) {
			e.printStackTrace();
			IEMainService.delete(mainId);
			IESendService.delete(sendId);
			IETableService.deleteByCons(" mainId =  " + mainId);
			IETableSearchService.deleteByCons(" mainId =  " + mainId);
			return 0;
		}
		
		return mainId;
	}

	public boolean deleteOldEmail(int mainId) throws Exception{
		return isDao.deleteIEmailByMainId(mainId);
	}
}
